<?php

/**
 * Toolkit.
 *
 * @package    flexiwik
 * @subpackage lib
 * @author     Jonathan Olger Nieto Lajo <jonathan_nieto@hotmail.com>
 */
class Toolkit extends sfToolkit
{
  private function __construct(){}
  
  public static function substr($string, $length = 15, $suffix = '...')
  {
    return strlen($string) > $length ? substr($string, 0, $length).$suffix : $string; 
  }
  
  public static function getSlugFromName($name)
  {
  	$search  = array('Á','É','Í','Ó','Ú','Ñ','á','é','í','ó','ú','ñ');
  	$replace = array('A','E','I','O','U','N','a','e','i','o','u','n');
  	
  	$name_url = preg_replace('/[^a-z0-9_]/', '_', strtolower(str_replace($search, $replace, $name)));
  	
  	return $name_url;
  }
  
  /**
   * Changes every end of line from CR or LF to CRLF.  Returns string.
   * @access private
   * @return string
   */
  public static function doFixEOL($string)
  {
    $string = str_replace("\r\n", "\n", $string);
    $string = str_replace("\r", "\n", $string);
    $string = str_replace("\n", '<br>', $string);
    return $string;
  }
	
  /**
   * Changes every CRLF to CR or LF.  Returns string.
   * @access private
   * @return string
   */
  public static function doUnfixEOL($string)
  {
    $string = str_replace("<br>", "\r\n", $string);
    return $string;
  }
  
  public static function getUrlFromTitle($title)
  {
  	//$search  = array('Á','É','Í','Ó','Ú','Ñ','á','é','í','ó','ú','ñ');
  	//$replace = array('A','E','I','O','U','N','a','e','i','o','u','n');
  	
  	//$name_url = preg_replace('/[^a-zA-Z0-9_]/', '_', strtolower(str_replace($search, $replace, $name)));
  	$url = preg_replace('/[^a-zA-Z0-9_\:\!]/', '_', $title);
  	
  	return $url;
  }
  
  public static function fixForUrl($string)
  {
  	return preg_replace('/[^a-zA-Z0-9_\:\!]/', '_', $string);
  }
  
  public static function encodeUrl($url)
  {
    return rawurlencode($url);
  }
  public static function encodeText($text)
  {
    return htmlspecialchars($text);
  }
  public static function decodeUrl($url)
  {
    return rawurldecode($url);
  }
  public static function fixEncodeUrl($string)
  {
    // $search  = array('%2F', '%3A');
  	// $replace = array('/'  , ':');
    $search  = array('%3A');
  	$replace = array(':');
  	
  	return str_replace($search, $replace, $string);
  }
  
  //***** OLDER *****//
  public static function getFilterNameFromNameUrl($url)
  {
  	return preg_replace('/\|/', ' ', $url);
  }
  
  public static function getEntranceRoute($module)
  {
    $uClass = sfInflector::underscore($module);
    $ns     = constant(sprintf('sfActionsExt::%s_NAMESPACE', strtoupper($uClass))); 
    
    return sfContext::getInstance()->getUser()->getAttribute('current_route', sprintf('@%s_lista', $uClass), $ns);
  }
  
  public static function formatNumber($number, $float = true, $decimals = 2, $dec_point = '.', $thousands_sep = '')
  {
  	return $float ? number_format($number, $decimals, $dec_point, $thousands_sep) : number_format($number);
  }
	
  public static function roundNumber($number = 0, $decimals = 2, $type = 'R')
  {
  	switch($type)
  	{
  	  case 'R' : $round_number = round($number, $decimals); break;
  	  case 'C' : $round_number = ceil($number); break;
  	  case 'F' : $round_number = floor($number); break;
  	  default  : $round_number = round($number, $decimals); break;
  	}
  	return sprintf("%01.".$decimals."f", $round_number);
  }
  
  //david tataje
  public static function generateUniqueNumber()//commented
  {
  	list($useg, $seg) = explode(' ', microtime());
  	$unique = $seg.substr($useg, 2, 2).rand(0, 999);
  	
  	return $unique;
  }
  public static function esc($str)//commented
  {
  	return strip_tags($str);
  }
  
  public static function getArrayFromObjects($objs, $methodKey, $methodValue)//commented
  {
  	$array = array();
  	foreach ($objs as $obj)
  	{
  	  $array[$obj->$methodKey()] = $obj->$methodValue();
  	}
  	
  	return $array;
  }
  
  public static function formatDate($date, $format = 'm-d-Y')
  {
    if ($date)
    {
      $date = explode('-', $date);
      //Deb::print_r($date);
      switch($format)
      {
        case 'Y-m'   : $date = sprintf('%s-%s', $date[1], $date[0]);break;
        case 'm-d'   : $date = sprintf('%s-%s', $date[0], $date[1]);break;//*** POCH 2009-03-04 ***//
        case '0m-0d' : $date = sprintf('%s-%s', kcDateTime::addCero2Number($date[0]), kcDateTime::addCero2Number($date[1]));break;  
        default      : $date = sprintf('%s-%s-%s', $date[2], $date[0], $date[1]);break;           
      }
    }
  
    return $date;
  }
   /* 
  public static function doMakeSeed()
  {
    list($usec, $sec) = explode(' ', microtime());
    return (float) $sec + ((float) $usec * 100000);
  }
  
  public static function multi_array_search($search_value, $the_array)
  {
    if (is_array($the_array))
    {
      foreach ($the_array as $key => $value)
      {
        $result = multi_array_search($search_value, $value);
        if (is_array($result))
        {
          $return = $result;
          array_unshift($return, $key);
          return $return;
        }
        elseif ($result == true)
        {
          $return[] = $key;
          return $return;
        }
      }
      return false;
    }
    else
    {
      if ($search_value == $the_array)
      {
         return true;
      }
      else
      {
        return false;
      }
    }
  }
	
  public static function roundNumber($number = 0, $decimals = 2, $type = 'R')
  {
  	switch($type)
  	{
  	  case 'R' : $round_number = round($number, $decimals); break;
  	  case 'C' : $round_number = ceil($number); break;
  	  case 'F' : $round_number = floor($number); break;
  	  default  : $round_number = round($number, $decimals); break;
  	}
  	return sprintf("%01.".$decimals."f", $round_number);
  }
  */
}